package model

import (
	"global/structs"
	"github.com/lunny/log"
	"time"
)

const (
	TRUST_COIN_STATUS_PENDING = iota
	TRUST_COIN_STATUS_PART
	TRUST_COIN_STATUS_ALL
	TRUST_COIN_STATUS_CANCEL
)

type trustCoinModel struct {}

var TrustCoinModel = new(trustCoinModel)

// 撤销委托
func (trustCoinModel) CancelTrusts(trustIds []int) (int64, bool) {
	trustCoin := new(structs.TrustCoin)
	trustCoin.Status = TRUST_COIN_STATUS_CANCEL
	trustCoin.UpdateTime = time.Now()
	affectedRows, err := MainDB.In("id", trustIds).Where("status=?", TRUST_COIN_STATUS_PENDING).Cols("status", "update_time").Update(trustCoin)
	if err != nil {
		log.Errorf("cancel trust error, msg: %s", err.Error())
		return 0, false
	}
	return affectedRows, affectedRows > 0
}

func (trustCoinModel) Get(tid int) (*structs.TrustCoin, error) {
	item := new(structs.TrustCoin)
	_, err := MainDB.Where("id=?", tid).Get(item)
	if err != nil {
		return nil, err
	}
	return item, nil
}